﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecopsy.Tools;
using AgencyEntityLib.BaseEntity;
using SqlLibrary;
using Ecopsy.DataTier.SqlLibrary;


public partial class iFrameAdmin_Employees : iAdmin_Page
{    
    wsFace ws_Face = (wsFace)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Face];

    #region Private
    
    private void MyBind()
    {
        ObjectDS_PF ODS = new ObjectDS_PF(GetList);        
        lvEmpl.DataSource = ODS.ImAsObjectDataSource;
        DataBind();        
    
    }
    private string DepartmentID
    {
        get { return Request.QueryString[qDepartmentID]; }
    }

    #endregion   
        
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {            
            MyBind();            

        }
    }
    
    protected void grdEmplList_Sorting(object sender, GridViewSortEventArgs e)
    {   
        Where.SortCondition.SortDirection = (Where.SortCondition.SortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending); ;
        Where.SortCondition.SortField = e.SortExpression;
        MyBind();

    }
    
    protected void grdMediaPoints_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }

    protected void EditConstracton(object sender, EventArgs e)
    {
        LinkButton bnt = (LinkButton)sender;
        Response.Redirect("iFrameAdmin_EditEmployeer.aspx?" + qDepartmentID + "=" + Request.QueryString[qDepartmentID] + "&" + qEmployerID + "=" + bnt.CommandArgument);    
        
    }

    //protected void EditButton_Click(object sender, EventArgs e)
    //{
    //    Button btn = (Button)sender;
    //    Response.Redirect("iFrameAdmin_EditEmployeer.aspx?" + qDepartmentID + "=" + Request.QueryString[qDepartmentID] + "&" + qEmployerID + "=" + btn.CommandArgument);    
    //}
    
    protected void AddButton_Click(object sender, EventArgs e)
    {
        string s ="iFrameAdmin_EditEmployeer.aspx?" + qDepartmentID + "=" + Request.QueryString[qDepartmentID] + "&" + qEmployerID + "=" + GlobalConstants.qPrefNEW_RECORD + Guid.NewGuid().ToString();        
        Response.Redirect(s);    
    }
    
    /// <summary>
    /// Поиск
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>    
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string sFlt = txtSearch.Text;
        int errPosition = -1;
        if (!ExpressionChecker.CheckStatment(ref sFlt, ref errPosition))
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "AlertBadFiltr", "alert('Ошибочное выражение фильтрации');", true);
            return;
        }

       ((EmployeeList.sqlLoadStatment)  Where.ListLoadStatment).Lexeme = sFlt;                  
        MyBind();
    }

    protected string GetTxtSearch()
    {
        //return txtSearch.Text; 
        return ((EmployeeList.sqlLoadStatment)Where.ListLoadStatment).Lexeme; 
    }

    public Employeer[] GetList(int startRowIndex, int maximumRows, out int RowCount)
    {
         
        ListWhere w = (ListWhere)Where;
        w.ListLoadStatment.StartRowIndex = startRowIndex;
        w.ListLoadStatment.EndRowIndex = startRowIndex + maximumRows;
        if (DepartmentID.Length > 0)
        {
            ((EmployeeList.sqlLoadStatment)w.ListLoadStatment).ParamValueList_SearchOption = vPropertyOfList.Options.Value; 
            ((EmployeeList.sqlLoadStatment)w.ListLoadStatment).ParamValueList.Clear();
            ((EmployeeList.sqlLoadStatment)w.ListLoadStatment).ParamValueList.Add(new vPropertyOfList.ParamListItem() { Param = string.Empty, Value = DepartmentID });            
            
        }

        EmployeeList l = ws_Face.GetEmplList  ((EmployeeList.sqlLoadStatment)w.ListLoadStatment);
        RowCount = l.OutputRwCnt;

        //заряжаем comparer функцией сортировки 
        Where.SortCondition.fnOrderBy = SortFunctions.fOrderBy;
        // Сортируем
        Employeer[] r=l.ToArray<Employeer>();
        Array.Sort( r, Where.SortCondition);
        return r;        
        
    }
    protected string GetDblClickFunction(string EmplId)
    {
        string s= "iFrameAdmin_EditEmployeer.aspx?" + qDepartmentID + "=" + Request.QueryString[qDepartmentID] + GlobalConstants.qParamSplitter  + qEmployerID + "=" + EmplId;
        return "document.location = 'PageProcessor.aspx?Page=" + s + "'";
        
            
    }
    

    protected Employeer  Get_Empl(string EmplId)
    {
        return ws_Face.GetEmpl (EmplId);
    }

    protected void lvEmpl_ItemEditing(object sender, ListViewEditEventArgs e)
    {

    }
    protected void lvEmpl_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {

    }
    protected void lvEmpl_PagePropertiesChanged(object sender, EventArgs e)
    {

    }
    protected void lvEmpl_ItemCommand(object sender, ListViewCommandEventArgs e)
    {

        ListView lv = (ListView)sender;
        switch (e.CommandName)
        {
            case "Edit":
                {

                    ListViewDataItem d = (ListViewDataItem)e.Item;
                    lv.EditIndex = this.DataPager.CalculateRealEditIndex(d.DataItemIndex);

                    break;
                }
            case "Insert":
                {

                    break;
                }
            case "Update":
                {
                    lv.EditIndex = -1;
                    break;
                }
            case "Delete":
                {
                    //DeleteCustomer(e.CommandArgument as string);
                    break;
                }
            case "Cancel":
                {
                    lv.EditIndex = -1;
                    break;
                }
        }

        MyBind();
        

    }


    protected override ListWhere Where
    {
        get
        {
            return f_Where (new EmployeeList.sqlLoadStatment(),"Name");
        }
    }

    protected  void lvSort(object sender, EventArgs e)
    {
        f_lvSort(sender, lvEmpl, MyBind); 
    }
  

}
